package com.game.manager;

import java.util.ArrayList;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import com.game.majiang.config.LogManager;
import com.game.majiang.vo.JackpotVo;
import com.game.server.db.entity.Jackpot;


public class JiangchiManager {

	private static  JiangchiManager instance;
	
	public  static JiangchiManager getInstance() {
		if (instance == null) {
			instance = new JiangchiManager();
		}
		return instance;
	}
	
	public JiangchiManager() {
//		threadPool.scheduleWithFixedDelay (new JackpotUpdate(), 10L,5L, TimeUnit.MINUTES);
	}
	
	public ArrayList<Jackpot> listJackpot = new ArrayList<>();
	
	ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(2);
	public synchronized int operationJiangchi(int type, int difen, int gold) {
		int money = 0;
//		switch (type) {
//		case 0:
//			addJiangchi(difen, gold);
//			break;
//		case 1:
//			money = reduceJiangchi(difen, gold);
//			break;
//		default:
//			break;
//		}
		return money;
	}
	
	
	
	//加奖池
	public void addJiangchi(int difen, int gold) {
		for (Jackpot jackpot : listJackpot) {
			if (jackpot.difen == difen) {
				jackpot.jiangchi+=gold;
				
				break;
			}
		}
	}
	//减奖池
	public int reduceJiangchi(int difen, int gold) {
		int shijiGold = 0;
		for (Jackpot jackpot : listJackpot) {
			if (jackpot.difen == difen) {
				if(jackpot.jiangchi >= gold) {
					jackpot.jiangchi -=gold;
					shijiGold = gold;
				} else {
					shijiGold = jackpot.jiangchi;
					jackpot.jiangchi = 0;
				}
				break;
				
			}
		}
		return shijiGold;
	}
	
	public int checkJiangchi(int difen) {
		for (Jackpot jackpot : listJackpot) {
			if (jackpot.difen == difen) {
				return jackpot.jiangchi;
				
			}
		}
		return 0;
	}
	
	class JackpotUpdate implements Runnable {  

		@Override  
		public void run() {  
			ArrayList<Jackpot> list = new ArrayList<>(listJackpot);
			for (Jackpot jackpot : list) {
				JackpotVo.update(jackpot.id, jackpot.jiangchi);
			}
			LogManager.getInstance().Log("JackpotUpdate");
		}  

	}
}
